var util = require('../../utils/util.js')

// index.js
// 获取应用实例
const app = getApp()
let shareTitle = null
let shareImg = null
let count = 10
let reCount = count

// 在页面中定义插屏广告
let interstitialAd = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexList: [],
    system: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getSystem().then(res => {
      app.globalData.system = res
      this.createInterstitialAd(res.chaping_ad) //创建插屏广告实例
      // console.log(res);
      this.setData({
        system: res
      })
    })
    this.getIndexList()
  },
  onShow() {
    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  //创建插屏广告实例
  createInterstitialAd(adUnitId) {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }
  },
  // 请求首页列表数据
  getIndexList(start = 0) {
    wx.showLoading()
    wx.cloud.database().collection("indexList").orderBy('id', "desc").skip(start).limit(count).get().then(res => {
      wx.hideLoading()
      reCount = res.data.length
      // console.log(res.data[0]);
      this.setData({
        indexList: this.data.indexList.concat(res.data)
      })
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    // console.log(reCount,'reCount')
    // console.log(this.data.indexList.length,'数组长度');
    if (reCount < count) {
      wx.showToast({
        icon: 'none',
        title: '没有数据了~',
      })
    } else {
      this.getIndexList(this.data.indexList.length)
    }

  },
  //表单处理
  formSubmit(e) {
    let data = e.detail.value
    let name = data.name
    let phone = data.phone
    let beizhu = data.beizhu
    if (name == '') {
      wx.showToast({
        icon: 'none',
        title: '姓名不能为空',
      })
      return
    }
    if (!this.validatemobile(phone)) {
      return
    }
    // 发送邮件，并把用户信息存入数据库
    wx.cloud.callFunction({
      name: 'sendEmail',
      data: {
        tag: 'fangyuan',
        pingtai: app.globalData.pingtai_tag,
        name,
        phone,
        beizhu,
        createTime: util.formatTime(new Date())
      }
    }).then(res => {}).catch(res => {
      wx.showToast({
        icon: 'error',
        title: '查询失败，稍后再试',
      })
    })
    // 存入数据库
    wx.cloud.database().collection('fanyuankehu').add({
      data: {
        pingtai: app.globalData.pingtai_tag,
        name,
        phone,
        beizhu,
        createTime: util.formatTime(new Date())
      }
    }).then(res => {}).catch(res => {})
    this.formReset()
    wx.showModal({
      title: '提示',
      content: '提交成功',
      cancelColor: 'cancelColor',
    })
  },
  formReset(e) {
    this.setData({
      inputValueName: '',
      inputValuePhone: '',
      inputValuebeizhu: ''
    })
  },
  // 检测手机号
  validatemobile(mobile) {
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    return true;
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    // console.log(e);
    if (e.from == 'button') {
      shareTitle = this.data.indexList[e.target.dataset.index].content
      shareImg = this.data.indexList[e.target.dataset.index].poster
    } else {
      shareTitle = this.data.indexList[0].content
      shareImg = this.data.indexList[0].poster
    }
    return {
      title: shareTitle,
      imageUrl: shareImg,
      path: `/pages/index/index`
    }
  },
  onShareTimeline(e) {
    shareTitle = this.data.indexList[0].content
    shareImg = this.data.indexList[0].poster
    return {
      title: shareTitle,
      imageUrl: shareImg
    }
  },
})